/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import controller.EventManager;
import entity.Event;
import entity.User;
import java.io.Serializable;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;


/**
 *
 * @author xubuntu
 */
@ManagedBean
@RequestScoped
public class CalendarBean implements Serializable{

    @ManagedProperty( value = "#{personalPageBean}" )
    private PersonalPageBean personalPageBean;
    @ManagedProperty( value = "#{navigationBean}" )
    private NavigationBean navigationBean;
    
    
    @EJB
    private EventManager evtMgr;
    
    private Date today = new Date();
    
    private List<Event> friendEventList;
    private Event eventSelected;
    private User user;
    @PostConstruct
    public void init() {
        user = personalPageBean.getUserSelected();
        
        eventSelected = new Event();
        friendEventList = evtMgr.getAllEventsTakePart(user);
        List<Event> organizedEvent= evtMgr.getAllEventsOrganized(user);
        friendEventList.addAll(organizedEvent);
        friendListControl();
        Collections.sort(friendEventList);
    }
    
    private void friendListControl(){

        int i = 0;
        while( i < friendEventList.size() ){
                if( friendEventList.get( i ).getDateStart().before(today))
                    friendEventList.remove( i );
                else
                    i++;
            }
    }

    public String whereToGo(){
        System.out.println("eventoselezionato"+eventSelected.getName());
        personalPageBean.setEventSelected(eventSelected);
        if(eventSelected.getTypePp()==0)
            return navigationBean.redirectToPrivatePagePage();
        return navigationBean.redirectToEventPage();
    }
    
    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public List<Event> getFriendEventList() {
        return friendEventList;
    }

    public void setFriendEventList(List<Event> friendEventList) {
        this.friendEventList = friendEventList;
    }

    public PersonalPageBean getPersonalPageBean() {
        return personalPageBean;
    }

    public void setPersonalPageBean(PersonalPageBean personalPageBean) {
        this.personalPageBean = personalPageBean;
    }

    public NavigationBean getNavigationBean() {
        return navigationBean;
    }

    public void setNavigationBean(NavigationBean navigationBean) {
        this.navigationBean = navigationBean;
    }

    public Event getEventSelected() {
        return eventSelected;
    }

    public void setEventSelected(Event eventSelected) {
        this.eventSelected = eventSelected;
    }
    
    
    
}
